Let us set some global options for all code chunks in this document.
# Set seed for reproducibility
set.seed(1982)
# Set global options for all code chunks
knitr::opts_chunk$set(
# Disable messages printed by R code chunks
message = TRUE,
# Disable warnings printed by R code chunks
warning = TRUE,
# Show R code within code chunks in output
echo = TRUE,
# Include both R code and its results in output
include = TRUE,
# Evaluate R code chunks
eval = TRUE,
# Enable caching of R code chunks for faster rendering
cache = FALSE,
# Align figures in the center of the output
fig.align = "center",
# Enable retina display for high-resolution figures
retina = 2,
# Show errors in the output instead of stopping rendering
error = TRUE,
# Do not collapse code and output into a single block
collapse = FALSE
)
# Start the figure counter
fig_count <- 0
# Define the captioner function
captioner <- function(caption) {
fig_count <<- fig_count + 1
paste0("Figure ", fig_count, ": ", caption)
}
# Define the function to truncate a number to two decimal places
truncate_to_two <- function(x) {
floor(x * 100) / 100
}
m1table <- rSPDE:::m1table
m2table <- rSPDE:::m2table
m3table <- rSPDE:::m3table
m4table <- rSPDE:::m4table# install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/testing"), dep=TRUE)
# inla.upgrade(testing = TRUE)
# remotes::install_github("inlabru-org/inlabru", ref = "devel")
# remotes::install_github("davidbolin/rspde", ref = "devel")
# remotes::install_github("davidbolin/metricgraph", ref = "devel")
library(INLA)## Loading required package: Matrix
## This is INLA_25.05.01 built 2025-05-01 18:43:33 UTC.
## - See www.r-inla.org/contact-us for how to get help.
## - List available models/likelihoods/etc with inla.list.models()
## - Use inla.doc(<NAME>) to access documentation
## - Consider upgrading R-INLA to testing[25.05.07] or stable[24.12.11].
## Loading required package: fmesher
## This is rSPDE 2.5.1
## - See https://davidbolin.github.io/rSPDE for vignettes and manuals.
## This is MetricGraph 1.4.1
## - See https://davidbolin.github.io/MetricGraph for vignettes and manuals.
##
## Attaching package: 'MetricGraph'
## The following object is masked from 'package:stats':
##
## filter
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# For each m and beta, this function returns c_m/b_{m+1} and the roots of rb and rc
my.get.roots <- function(order, beta) {
mt <- get(paste0("m", order, "table"))
rb <- rep(0, order + 1)
rc <- rep(0, order)
if(order == 1) {
rc = approx(mt$beta, mt[[paste0("rc")]], beta)$y
} else {
rc = sapply(1:order, function(i) {
approx(mt$beta, mt[[paste0("rc.", i)]], beta)$y
})
}
rb = sapply(1:(order+1), function(i) {
approx(mt$beta, mt[[paste0("rb.", i)]], xout = beta)$y
})
factor = approx(mt$beta, mt$factor, xout = beta)$y
return(list(rb = rb, rc = rc, factor = factor))
}
# Function the polynomial coefficients in increasing order like a+bx+cx^2+...
poly_from_roots <- function(roots) {
coef <- 1
for (r in roots) {coef <- convolve(coef, c(1, -r), type = "open")}
return(coef)
}
# Function to compute the partial fraction parameters
compute_partial_fraction_param <- function(factor, pr_roots, pl_roots, cte) {
pr_coef <- c(0, poly_from_roots(pr_roots))
pl_coef <- poly_from_roots(pl_roots)
factor_pr_coef <- pr_coef
pr_plus_pl_coef <- factor_pr_coef + cte/factor * pl_coef
res <- gsignal::residue(factor_pr_coef, pr_plus_pl_coef)
return(list(r = res$r, p = res$p, k = res$k))
}
# Function to compute the fractional operator
my.fractional.operators.frac <- function(L, beta, C, scale.factor, m = 1, time_step) {
C <- Matrix::Diagonal(dim(C)[1], rowSums(C))
Ci <- Matrix::Diagonal(dim(C)[1], 1 / rowSums(C))
I <- Matrix::Diagonal(dim(C)[1])
L <- L / scale.factor
LCi <- L %*% Ci
if(beta == 1){
return(list(Ci = Ci, C = C, LCi = LCi, L = L, m = m, beta = beta, LHS = C + time_step * L))
} else {
roots <- my.get.roots(m, beta)
poles_rs_k <- compute_partial_fraction_param(roots$factor, roots$rc, roots$rb, time_step)
partial.fraction.factors <- list()
for (i in 1:(m+1)) {partial.fraction.factors[[i]] <- (LCi - poles_rs_k$p[i] * I)/poles_rs_k$r[i]}
partial.fraction.factors[[m+2]] <- ifelse(is.null(poles_rs_k$k), 0, poles_rs_k$k) * I
return(list(Ci = Ci, C = C, LCi = LCi, L = L, m = m, beta = beta, partial.fraction.factors = partial.fraction.factors))
}
}
# Function to solve the iteration
my.solver.frac <- function(obj, v){
beta <- obj$beta
m <- obj$m
C <- obj$C
Ci <- obj$Ci
if (beta == 1){
return(solve(obj$LHS, v))
} else {
partial.fraction.factors <- obj$partial.fraction.factors
output <- partial.fraction.factors[[m+2]] %*% v
for (i in 1:(m+1)) {output <- output + solve(partial.fraction.factors[[i]], v)}
return(Ci %*% output)
}
}
# Function to build a tadpole graph and create a mesh
gets_graph_tadpole <- function(h){
edge1 <- rbind(c(0,0),c(1,0))
theta <- seq(from=-pi,to=pi,length.out = 10000)
edge2 <- cbind(1+1/pi+cos(theta)/pi,sin(theta)/pi)
edges = list(edge1, edge2)
graph <- metric_graph$new(edges = edges)
graph$set_manual_edge_lengths(edge_lengths = c(1,2))
graph$build_mesh(h = h)
return(graph)
}
# Function to compute the eigenfunctions
tadpole.eig <- function(k,graph){
x1 <- c(0,graph$get_edge_lengths()[1]*graph$mesh$PtE[graph$mesh$PtE[,1]==1,2])
x2 <- c(0,graph$get_edge_lengths()[2]*graph$mesh$PtE[graph$mesh$PtE[,1]==2,2])
if(k==0){
f.e1 <- rep(1,length(x1))
f.e2 <- rep(1,length(x2))
f1 = c(f.e1[1],f.e2[1],f.e1[-1], f.e2[-1])
f = list(phi=f1/sqrt(3))
} else {
f.e1 <- -2*sin(pi*k*1/2)*cos(pi*k*x1/2)
f.e2 <- sin(pi*k*x2/2)
f1 = c(f.e1[1],f.e2[1],f.e1[-1], f.e2[-1])
if((k %% 2)==1){
f = list(phi=f1/sqrt(3))
} else {
f.e1 <- (-1)^{k/2}*cos(pi*k*x1/2)
f.e2 <- cos(pi*k*x2/2)
f2 = c(f.e1[1],f.e2[1],f.e1[-1],f.e2[-1])
f <- list(phi=f1,psi=f2/sqrt(3/2))
}
}
return(f)
}
# Function to order the vertices for plotting
order_to_plot <- function(v, graph){
edge_number <- graph$mesh$VtE[, 1]
pos <- sum(edge_number == 1)+1
return(c(v[1], v[3:pos], v[2], v[(pos+1):length(v)], v[2]))
}We want to solve the fractional diffusion equation \[\begin{equation} \label{eq:maineq} \partial_t u+(\kappa^2-\Delta_\Gamma)^{\alpha/2} u=f \text { on } \Gamma \times(0, T), \quad u(0)=u_0 \text { on } \Gamma, \end{equation}\] where \(u\) satisfies the Kirchhoff vertex conditions \[\begin{equation} \label{eq:Kcond} \left\{\phi\in C(\Gamma)\;\Big|\; \forall v\in V: \sum_{e\in\mathcal{E}_v}\partial_e \phi(v)=0 \right\} \end{equation}\] The solution is given by \[\begin{equation} \label{eq:sol_reprentation} u(s,t) = \displaystyle\sum_{j\in\mathbb{N}}e^{-\lambda^{\alpha/2}_jt}\left(u_0, e_j\right)_{L_2(\Gamma)}e_j(s) + \int_0^t \displaystyle\sum_{j\in\mathbb{N}}e^{-\lambda^{\alpha/2}_j(t-r)}\left(f(\cdot, r), e_j\right)_{L_2(\Gamma)}e_j(s)dr. \end{equation}\]
If we choose \(w_j\) and \(v_j\) and take the initial condition and the right hand side funciton as
\[\begin{equation} u_0(s) = \sum_{j=0}^{N} w_j e_j(s) \text{ and so } \left(u_0, e_j\right)_{L_2(\Gamma)} = w_j, \end{equation}\] \[\begin{equation} \text{In matrix notation: } \quad\boldsymbol{U}_0 = \boldsymbol{E}^N_h\boldsymbol{c}, \quad \boldsymbol{E}^N_h = \left[e_0, e_1, \ldots, e_{N}\right], \quad \boldsymbol{c} = \left[w_0, w_1, \ldots, w_{N}\right]^\top, \end{equation}\] and \[\begin{equation} f(s,t) = \sum_{j=0}^{M} v_j e^{-\lambda^{\alpha/2}_jt} e_j(s) \text{ and so } \left(f(\cdot,r), e_j\right)_{L_2(\Gamma)} = v_j e^{-\lambda^{\alpha/2}_jr}, \end{equation}\] \[\begin{equation} \text{In matrix notation: } \quad\boldsymbol{f} = \boldsymbol{E}^M_h \boldsymbol{V}, \quad \boldsymbol{V}_{ji} = v_je^{-\lambda^{\alpha/2}_jt_i} \end{equation}\]
then the solution is given by \[\begin{align} u(s,t) &= \displaystyle\sum_{j=0}^{N}w_je^{-\lambda^{\alpha/2}_jt}e_j(s) + \int_0^t \displaystyle\sum_{j=0}^Me^{-\lambda^{\alpha/2}_j(t-r)}v_j e^{-\lambda^{\alpha/2}_jr}e_j(s)dr\\ &= \displaystyle\sum_{j=0}^{N}w_je^{-\lambda^{\alpha/2}_jt}e_j(s) + t \displaystyle\sum_{j=0}^Mv_j e^{-\lambda^{\alpha/2}_jt}e_j(s) \end{align}\]
\[\begin{equation} \text{In matrix notation: } \quad\boldsymbol{U} =\boldsymbol{E}^N_h \boldsymbol{W} + \boldsymbol{f}\boldsymbol{t}, \quad \boldsymbol{W}_{ji} = w_je^{-\lambda^{\alpha/2}_jt_i},\quad \boldsymbol{t} = \left[t_0, t_1, \ldots, t_{K}\right]^\top \end{equation}\]
## Starting graph creation...
## LongLat is set to FALSE
## Creating edges...
## Setting edge weights...
## Computing bounding box...
## Setting up edges
## Merging close vertices
## Total construction time: 0.17 secs
## Creating and updating vertices...
## Storing the initial graph...
## Computing the relative positions of the edges...
T_final <- 2
time_step <- 0.01
time_seq <- seq(0, T_final, by = time_step)
# Compute the FEM matrices
graph$compute_fem()
G <- graph$mesh$G
C <- graph$mesh$C
I <- Matrix::Diagonal(nrow(C))
x <- graph$mesh$V[, 1]
y <- graph$mesh$V[, 2]
weights <- graph$mesh$weights
kappa <- 1
alpha <- 0.5 # from 0.5 to 2
m = 1
beta <- alpha/2
L <- kappa^2*C + G
# Parameters to construct U_0
N_finite <- 4 # choose an even number
adjusted_N_finite <- N_finite + N_finite/2 + 1
EIGENVAL_ALPHA <- NULL
EIGENFUN <- NULL
INDEX <- NULL
PHI_OR_PSI <- NULL
for (j in 0:N_finite) {
lambda_j_alpha <- (kappa^2 + (j*pi/2)^2)^(alpha/2)
e_j <- tadpole.eig(j,graph)$phi
EIGENVAL_ALPHA <- c(EIGENVAL_ALPHA, lambda_j_alpha)
EIGENFUN <- cbind(EIGENFUN, e_j)
INDEX <- c(INDEX, j)
PHI_OR_PSI <- c(PHI_OR_PSI, "phi")
if (j>0 && (j %% 2 == 0)) {
lambda_j_alpha <- (kappa^2 + (j*pi/2)^2)^(alpha/2)
e_j <- tadpole.eig(j,graph)$psi
EIGENVAL_ALPHA <- c(EIGENVAL_ALPHA, lambda_j_alpha)
EIGENFUN <- cbind(EIGENFUN, e_j)
INDEX <- c(INDEX, j)
PHI_OR_PSI <- c(PHI_OR_PSI, "psi")
}
}
# Building the initial condition as \sum coeff_j EIGENFUN_j
coeff <- 50*(1:length(EIGENVAL_ALPHA))^-1
coeff[-5] <- 0
U_0 <- EIGENFUN %*% coeff
U_true <- EIGENFUN %*% outer(1:length(EIGENVAL_ALPHA),
1:length(time_seq),
function(i, j) coeff[i] * exp(-EIGENVAL_ALPHA[i] * time_seq[j]))
c_k <- 10
what_eigenfunction_for_ff <- 7
ff <- function(t){
return(c_k*EIGENFUN[,what_eigenfunction_for_ff]*exp(-t*EIGENVAL_ALPHA[what_eigenfunction_for_ff]))
}
FF_true <- matrix(NA, nrow = length(x), ncol = length(time_seq))
FF_sol_true <- matrix(NA, nrow = length(x), ncol = length(time_seq))
for (k in 1:length(time_seq)) {
FF_true[, k] <- ff(time_seq[k]) # this is the right hand side function
FF_sol_true[, k] <- time_seq[k]*FF_true[, k] # this is the second term in the solution
}
U_true <- U_true + FF_sol_true
graph_to_approx_int <- gets_graph_tadpole(h = 0.001)## Starting graph creation...
## LongLat is set to FALSE
## Creating edges...
## Setting edge weights...
## Computing bounding box...
## Setting up edges
## Merging close vertices
## Total construction time: 0.16 secs
## Creating and updating vertices...
## Storing the initial graph...
## Computing the relative positions of the edges...
loc_finer <- graph_to_approx_int$get_mesh_locations()
A <- graph$fem_basis(loc_finer)
graph_to_approx_int$compute_fem()
C_finer <- graph_to_approx_int$mesh$C
EIGENFUN_FOR_FF <- tadpole.eig(INDEX[what_eigenfunction_for_ff], graph_to_approx_int)
if (PHI_OR_PSI[what_eigenfunction_for_ff] == "phi"){
eigenfun_for_ff <- EIGENFUN_FOR_FF$phi
} else if (PHI_OR_PSI[what_eigenfunction_for_ff] == "psi"){
eigenfun_for_ff <- EIGENFUN_FOR_FF$psi
}
int_basis_eigen <- as.vector(t(as.matrix(eigenfun_for_ff)) %*% C_finer %*% A)
COEF <- c_k*exp(-time_seq*EIGENVAL_ALPHA[what_eigenfunction_for_ff])
FF_approx <- int_basis_eigen %*% t(COEF){r}
coarse_h <- 0.1
coarse_graph <- gets_graph_tadpole(h = coarse_h)
coarse_A <- coarse_graph$fem_basis(graph$get_mesh_locations())
coarse_time_step <- 0.1
coarse_time_seq <- seq(0, T_final, by = coarse_time_step)
# Compute the FEM matrices
coarse_graph$compute_fem()
coarse_G <- coarse_graph$mesh$G
coarse_C <- coarse_graph$mesh$C
coarse_I <- Matrix::Diagonal(nrow(coarse_C))
coarse_x <- coarse_graph$mesh$V[, 1]
coarse_y <- coarse_graph$mesh$V[, 2]
coarse_edge_number <- coarse_graph$mesh$VtE[, 1]
coarse_pos <- sum(coarse_edge_number == 1)+1
coarse_order_to_plot <- function(v)return(c(v[1], v[3:coarse_pos], v[2], v[(coarse_pos+1):length(v)], v[2]))
coarse_weights <- coarse_graph$mesh$weights
coarse_L <- kappa^2*coarse_C + coarse_G
coarse_U_0 <- solve(t(coarse_A) %*% coarse_A, t(coarse_A) %*% U_0)
coarse_x <- coarse_order_to_plot(coarse_x)
coarse_y <- coarse_order_to_plot(coarse_y)
plot_ly(x = ~order_to_plot(x), y = ~order_to_plot(y), z = ~apply(U_0, 2,order_to_plot)[,1], type = 'scatter3d', mode = 'lines')
plot_ly(x = ~coarse_x, y = ~coarse_y, z = ~apply(coarse_U_0, 2, coarse_order_to_plot)[,1], type = 'scatter3d', mode = 'lines')
{r}
coarse_my_op_frac <- my.fractional.operators.frac(coarse_L, beta, coarse_C, scale.factor = kappa^2, m = m, coarse_time_step)
U_approx2 <- matrix(NA, nrow = nrow(coarse_C), ncol = length(coarse_time_seq))
U_approx2[, 1] <- U_0
# Time-stepping loop
for (k in 1:(length(coarse_time_seq) - 1)) {
U_approx2[, k + 1] <- as.matrix(my.solver.frac(coarse_my_op_frac, coarse_my_op_frac$C %*% U_approx2[, k] + coarse_time_step * FF_approx[, k + 1]))
}
my_op_frac <- my.fractional.operators.frac(L, beta, C, scale.factor = kappa^2, m = m, time_step)
U_approx2 <- matrix(NA, nrow = nrow(C), ncol = length(time_seq))
U_approx2[, 1] <- U_0
# Time-stepping loop
for (k in 1:(length(time_seq) - 1)) {
U_approx2[, k + 1] <- as.matrix(my.solver.frac(my_op_frac, my_op_frac$C %*% U_approx2[, k] + time_step * FF_approx[, k + 1]))
}#U_approx2 <- U_approx1
U_approx1 <- U_approx2
mean_w <- function(v){return(mean(v*weights))}
max_error_at_each_time1 <- apply((U_true - U_approx1)^2, 2, mean)
max_error_at_each_time2 <- apply((U_true - U_approx2)^2, 2, mean)
max_error_between_both_approx <- apply((U_approx1 - U_approx2)^2, 2, mean)
error1 <- sqrt(time_step * sum(max_error_at_each_time1))
error2 <- sqrt(time_step * sum(max_error_at_each_time2))
errorb <- sqrt(time_step * sum(max_error_between_both_approx))
x <- order_to_plot(x, graph)
y <- order_to_plot(y, graph)
U_true <- apply(U_true, 2, order_to_plot, graph = graph)
U_approx1 <- apply(U_approx1, 2, order_to_plot, graph = graph)
U_approx2 <- apply(U_approx2, 2, order_to_plot, graph = graph)
# Create interactive plot
fig <- plot_ly()
# Add first line (max_error_at_each_time1)
fig <- fig %>% add_trace(
x = ~time_seq, y = ~max_error_at_each_time1, type = 'scatter', mode = 'lines+markers',
line = list(color = 'red', width = 2),
marker = list(color = 'red', size = 4),
name = paste0("Error True and Approx 1: ", sprintf("%.3e", error1))
)
# Add second line (max_error_at_each_time2)
fig <- fig %>% add_trace(
x = ~time_seq, y = ~max_error_at_each_time2, type = 'scatter', mode = 'lines+markers',
line = list(color = 'blue', width = 2, dash = "dot"),
marker = list(color = 'blue', size = 4),
name = paste0("Error True and Approx 2: ", sprintf("%.3e", error2))
)
# Add third line (max_error_between_both_approx)
fig <- fig %>% add_trace(
x = ~time_seq, y = ~max_error_between_both_approx, type = 'scatter', mode = 'lines+markers',
line = list(color = 'orange', width = 2),
marker = list(color = 'orange', size = 4),
name = paste0("Error Between Approximations: ", sprintf("%.3e", errorb))
)
# Layout
fig <- fig %>% layout(
title = "Error at Each Time Step",
xaxis = list(title = "Time"),
yaxis = list(title = "Error"),
legend = list(x = 0.1, y = 0.9)
)
plot_data <- data.frame(
x = rep(x, times = ncol(U_true)),
y = rep(y, times = ncol(U_true)),
z_true = as.vector(U_true),
z_approx1 = as.vector(U_approx1),
z_approx2 = as.vector(U_approx2),
frame = rep(time_seq, each = length(x))
)
# Compute axis limits
x_range <- range(x)
y_range <- range(y)
z_range <- range(c(U_true, U_approx1, U_approx2))
# Initial plot setup (first frame only)
p <- plot_ly(plot_data, frame = ~frame) %>%
add_trace(
x = ~x, y = ~y, z = ~z_true,
type = "scatter3d", mode = "lines",
name = "True",
line = list(color = "green", width = 2)
) %>%
add_trace(
x = ~x, y = ~y, z = ~z_approx1,
type = "scatter3d", mode = "lines",
name = "Approx 1",
line = list(color = "red", width = 2)
) %>%
add_trace(
x = ~x, y = ~y, z = ~z_approx2,
type = "scatter3d", mode = "lines",
name = "Approx 2",
line = list(color = "blue", width = 2)
) %>%
layout(
scene = list(
xaxis = list(title = "x", range = x_range),
yaxis = list(title = "y", range = y_range),
zaxis = list(title = "Value", range = z_range),
aspectratio = list(x = 2.4, y = 1.2, z = 1.2),
camera = list(
eye = list(x = 1.5, y = 1.5, z = 1), # Adjust the viewpoint
center = list(x = 0, y = 0, z = 0))
),
updatemenus = list(
list(
type = "buttons", showactive = FALSE,
buttons = list(
list(label = "Play", method = "animate",
args = list(NULL, list(frame = list(duration = 100, redraw = TRUE), fromcurrent = TRUE))),
list(label = "Pause", method = "animate",
args = list(NULL, list(mode = "immediate", frame = list(duration = 0), redraw = FALSE)))
)
)
),
title = "Time: 0"
)
# Convert to plotly object with frame info
pb <- plotly_build(p)
# Inject custom titles into each frame
for (i in seq_along(pb$x$frames)) {
t <- time_seq[i]
err <- signif(max_error_between_both_approx[i], 4)
pb$x$frames[[i]]$layout <- list(title = paste0("Time: ", t, " | Error: ", err))
}## This is to plot the right hand side alone
FF_true <- apply(FF_true, 2, order_to_plot, graph = graph)
FF_sol_true <- apply(FF_sol_true, 2, order_to_plot, graph = graph)
FF_approx <- apply(FF_approx, 2, order_to_plot, graph = graph)
plot_data <- data.frame(
x = rep(x, times = ncol(FF_true)),
y = rep(y, times = ncol(FF_true)),
ff_true = as.vector(FF_true),
ff_sol_true = as.vector(FF_sol_true),
ff_approx = as.vector(FF_approx),
frame = rep(time_seq, each = length(x))
)
# Compute axis limits
x_range <- range(x)
y_range <- range(y)
z_range <- range(c(FF_true, FF_sol_true, FF_approx))
# Initial plot setup (first frame only)
p_ff <- plot_ly(plot_data, frame = ~frame) %>%
add_trace(
x = ~x, y = ~y, z = ~ff_true,
type = "scatter3d", mode = "lines",
name = "f(s,t)",
line = list(color = "green", width = 2)
) %>%
add_trace(
x = ~x, y = ~y, z = ~ff_sol_true,
type = "scatter3d", mode = "lines",
name = "tf(s,t) = u(s,t)-SOL(u_0)",
line = list(color = "red", width = 2)
) %>%
add_trace(
x = ~x, y = ~y, z = ~ff_approx,
type = "scatter3d", mode = "lines",
name = "F^k = (f^k, phi)",
line = list(color = "blue", width = 2)
) %>%
layout(
scene = list(
xaxis = list(title = "x", range = x_range),
yaxis = list(title = "y", range = y_range),
zaxis = list(title = "Value", range = z_range),
aspectratio = list(x = 2.4, y = 1.2, z = 1.2),
camera = list(
eye = list(x = 1.5, y = 1.5, z = 1), # Adjust the viewpoint
center = list(x = 0, y = 0, z = 0))
),
updatemenus = list(
list(
type = "buttons", showactive = FALSE,
buttons = list(
list(label = "Play", method = "animate",
args = list(NULL, list(frame = list(duration = 100, redraw = TRUE), fromcurrent = TRUE))),
list(label = "Pause", method = "animate",
args = list(NULL, list(mode = "immediate", frame = list(duration = 0), redraw = FALSE)))
)
)
),
title = "Time: 0"
)
# Convert to plotly object with frame info
pb_ff <- plotly_build(p_ff)
# Inject custom titles into each frame
for (i in seq_along(pb_ff$x$frames)) {
t <- time_seq[i]
pb_ff$x$frames[[i]]$layout <- list(title = paste0("Time: ", t))
}Figure 1: Caption